﻿﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;  
using System.Data; 
using UFIDA.U8.DB;

namespace UFIDA.U8.WAZH.NBBankSync
{
    public class accvouchHelper 
    {
        public List<CommandInfo> commands = new List<CommandInfo>();
        public string errstr = "";
        protected override void Create(U8Login.clsLogin u8Login, List<JsonModel.accvouch> models)
        {
            if (models.Count != 1)
            {
                //if (models.Count > 1)
                //{
                //    throw new Exception("目前不支持项目档案的批量业务!");
                //}
                if (models.Count == 0)
                {
                    throw new Exception("不允许传空!");
                }
            }
            else if (models.Count == 1)
            {
                if (models.Count == 0)
                {
                    throw new Exception("该凭证只有1条分录!");
                }
            }
            StringBuilder strsql = new StringBuilder(); 
            strsql.Append(" select  isnull(Max(ino_id),0)+1 as iRecordID from GL_accvouch where iperiod=" + models[0].dbill_date.Month + " and iyear=" + models[0].dbill_date.Year + " and csign='" + models[0].csign + "'");
            string mdcode = "";
            string mccode = "";
            for (int i = 0; i < models.Count; i++)
            {
                StringBuilder strsql1 = new StringBuilder(); 
                if (models[i].md != 0)
                {
                    mdcode = mdcode + models[i].ccode + ",";
                }
                if (models[i].mc != 0)
                {
                    mccode = mccode + models[i].ccode + ",";
                }
            }
            if (mccode == "" || mdcode == "")
            { 
                mccode = "";
                mdcode = "";
            }
            else
            {
                mdcode = mdcode.Substring(0, mdcode.Length - 1);
                mccode = mccode.Substring(0, mccode.Length - 1);
            }


            DataSet ds = DbHelperSQL.Query(strsql.ToString(), u8Login.UFDataConnstringForNet);
            if (ds.Tables[0].DefaultView.Count == 0)
            {
                throw new Exception("单据最大值不存在！");
            }

            if (UFIDA.U8.DB.DAL.GL_accvouch.Exists(models[0].ino_id, models[0].dbill_date.Month, models[0].dbill_date.Year, models[0].csign, base.U8Constr))
            {
                StringBuilder strsqlino = new StringBuilder(); 
                strsqlino.Append("select * from GL_accvouch");
                strsqlino.Append(" where cDefine1='" + models[0].ino_id + "' and iperiod=" + models[0].dbill_date.Month + " and iyear=" + models[0].dbill_date.Year + " and csign='" + models[0].csign + "'");
                DataSet dsino = DbHelperSQL.Query(strsqlino.ToString(), u8Login.UFDataConnstringForNet); 
                #region 更新凭证
                
                #endregion
                #region 删除后
                if (dsino.Tables[0].DefaultView[0]["ccheck"] != DBNull.Value || dsino.Tables[0].DefaultView[0]["cbook"] != DBNull.Value || dsino.Tables[0].DefaultView[0]["ibook"].ToString() != "0")
                {
                    throw new Exception("凭证已审核或记账,在U8的" + models[0].csign + "的" + dsino.Tables[0].DefaultView[0]["ino_id"].ToString() + ",若需重新传，请弃审或反记账后重新传");
                }
                if (UFIDA.U8.DB.DAL.GL_CashTable.Exists(dsino.Tables[0].DefaultView[0]["ino_id"].ToString(), models[0].dbill_date.Month, models[0].dbill_date.Year, models[0].csign, base.U8Constr))
                {
                    commands.Add(UFIDA.U8.DB.DAL.GL_CashTable.DeleteCommand(dsino.Tables[0].DefaultView[0]["ino_id"].ToString(), models[0].dbill_date.Month, models[0].dbill_date.Year, models[0].csign));
                }
                commands.Add(UFIDA.U8.DB.DAL.accvouch.Delete(models[0].ino_id, models[0].dbill_date.Month, models[0].dbill_date.Year, models[0].csign));
                DbHelperSQL.ExecuteSqlTran(commands, base.U8Constr);
                strsql = new StringBuilder();
                strsql.Append(" select  isnull(Max(ino_id),0)+1 as iRecordID from GL_accvouch where iperiod=" + models[0].dbill_date.Month + " and iyear=" + models[0].dbill_date.Year + " and csign='" + models[0].csign + "'");
                ds = DbHelperSQL.Query(strsql.ToString(), u8Login.UFDataConnstringForNet);
                commands = new List<CommandInfo>();
                add(models, u8Login, commands);
          
                billid = models[0].dbill_date.Year.ToString() + models[0].dbill_date.Month.ToString("00") + "-" + models[0].csign + "-" + ds.Tables[0].DefaultView[0]["iRecordID"].ToString();
                #endregion
            }
            else
            { 
                add(models, u8Login, commands);  
              
                billid = models[0].dbill_date.Year.ToString() + models[0].dbill_date.Month.ToString("00") + "-" + models[0].csign + "-" + ds.Tables[0].DefaultView[0]["iRecordID"].ToString();
            }
        }
        public void add(List<JsonModel.accvouch> models, U8Login.clsLogin u8Login, List<CommandInfo> commands)
        {
            try
            {
                StringBuilder strsql = new StringBuilder();
                strsql.Append(" select  isnull(Max(ino_id),0)+1 as iRecordID from GL_accvouch where iperiod=" + models[0].dbill_date.Month + " and iyear=" + models[0].dbill_date.Year + " and csign='" + models[0].csign + "'");
                string mdcode = "";
                string mccode = "";
                for (int i = 0; i < models.Count; i++)
                {
                    StringBuilder strsql1 = new StringBuilder();
                    if (models[i].md != 0)
                    {
                        mdcode = mdcode + models[i].ccode + ",";
                    }
                    if (models[i].mc != 0)
                    {
                        mccode = mccode + models[i].ccode + ",";
                    }
                }
                if (mccode == "" || mdcode == "")
                {
                    mccode = "";
                    mdcode = "";
                }
                else
                {
                    mdcode = mdcode.Substring(0, mdcode.Length - 1);
                    mccode = mccode.Substring(0, mccode.Length - 1);
                }

                string errstr = "";
                DataSet ds = DbHelperSQL.Query(strsql.ToString(), u8Login.UFDataConnstringForNet);
                if (ds.Tables[0].DefaultView.Count == 0)
                {
                    throw new Exception("单据最大值不存在！");
                }
                for (int i = 0; i < models.Count; i++)
                { 
                    JsonModel.accvouch model = models[i];
                    UFIDA.U8.DB.Model.accvouch accvouch = new UFIDA.U8.DB.Model.accvouch();

                    accvouch.iperiod = model.dbill_date.Month;
                    accvouch.iyear = model.dbill_date.Year;
                    accvouch.iYPeriod = Convert.ToInt32(model.dbill_date.Year.ToString() + model.dbill_date.Month.ToString("00"));
                    accvouch.ino_id = Convert.ToInt32(ds.Tables[0].DefaultView[0]["iRecordID"].ToString());
                    accvouch.cDefine2 = model.cn_id;
                    if (Convert.ToString(model.dt_date) != "0001-01-01 0:00:00")
                    {
                        accvouch.dt_date = model.dt_date; 
                    }
                    string str = model.dt_date.ToString();
                    DataSet dscsign = null;
                    DataSet dsisignseq = null;
                    if (model.csign.ToString() != "")
                    {
                        strsql = new StringBuilder();
                        strsql.Append("select * from dsign");
                        strsql.Append(" where csign='" + model.csign + "'");
                        dsisignseq = DbHelperSQL.Query(strsql.ToString(), u8Login.UFDataConnstringForNet);
                        if (dsisignseq.Tables[0].DefaultView.Count == 0)
                        {
                            throw new Exception("第" + (i + 1) + "行凭证类别" + model.csign + "不存在对应U8凭证类别排序号");
                        }
                        accvouch.isignseq = dsisignseq.Tables[0].DefaultView[0]["isignseq"].ObjToInt32();
                        accvouch.csign = model.csign;

                    } 
                    accvouch.dbill_date = model.dbill_date;
                    accvouch.idoc = model.idoc;
                    accvouch.inid = model.inid;
                    if (model.ccode.ToString() != "")
                    {
                        accvouch.ccode = model.ccode;
                        strsql = new StringBuilder();
                        strsql.Append("select * from code");
                        strsql.Append(" where ccode='" + model.ccode + "' and iyear='" + model.dbill_date.Year + "'");
                        DataSet dscode = DbHelperSQL.Query(strsql.ToString(), u8Login.UFDataConnstringForNet);
                        if (dscode.Tables[0].DefaultView.Count == 0)
                        {
                            errstr = errstr + "第" + (i + 1) + "行科目编码" + model.ccode + "不存在U8科目编码|";
                            continue;
                        }
                        if (dscode.Tables[0].DefaultView[0]["bperson"].ObjToBoolean() == true)
                        {
                            if (model.cperson_id.ToString() != "")
                            {
                                strsql = new StringBuilder();
                                strsql.Append("select * from person");
                                strsql.Append(" where cPersonCode='" + model.cperson_id + "'");
                                DataSet dsPerson = DbHelperSQL.Query(strsql.ToString(), u8Login.UFDataConnstringForNet);
                                if (dsPerson.Tables[0].DefaultView.Count == 0)
                                {
                                    throw new Exception("第" + (i + 1) + "行数据U8人员编码" + model.cperson_id + "不存在对应U8人员编码");
                                }
                                accvouch.cperson_id = model.cperson_id;
                            }
                        }
                        if (dscode.Tables[0].DefaultView[0]["bcus"].ObjToBoolean() == true)
                        {
                            if (model.ccus_id.ToString() != "")
                            {
                                strsql = new StringBuilder();
                                strsql.Append("select * from customer");
                                strsql.Append(" where ccuscode='" + model.ccus_id + "'");
                                DataSet dsPerson = DbHelperSQL.Query(strsql.ToString(), u8Login.UFDataConnstringForNet);
                                if (dsPerson.Tables[0].DefaultView.Count == 0)
                                {
                                    throw new Exception("第" + (i + 1) + "行数据客户编码" + model.ccus_id + "不存在对应U8客户档案");
                                }
                                accvouch.ccus_id = model.ccus_id;
                            }
                        }
                        if (dscode.Tables[0].DefaultView[0]["bsup"].ObjToBoolean() == true)
                        {
                            if (model.csup_id.ToString() != "")
                            {
                                strsql = new StringBuilder();
                                strsql.Append("select * from vendor");
                                strsql.Append(" where cvencode='" + model.csup_id + "'");
                                DataSet dsPerson = DbHelperSQL.Query(strsql.ToString(), u8Login.UFDataConnstringForNet);
                                if (dsPerson.Tables[0].DefaultView.Count == 0)
                                {
                                    throw new Exception("第" + (i + 1) + "行数据供应商编码" + model.csup_id + "不存在对应U8供应商档案");
                                }
                                accvouch.csup_id = model.csup_id;
                            }
                        }
                        if (dscode.Tables[0].DefaultView[0]["bdept"].ObjToBoolean() == true)
                        {
                            if (model.cdept_id.ToString() != "")
                            {
                                strsql = new StringBuilder();
                                strsql.Append("select * from department ");
                                strsql.Append(" where cdepcode='" + model.cdept_id + "'");
                                DataSet dsPerson = DbHelperSQL.Query(strsql.ToString(), u8Login.UFDataConnstringForNet);
                                if (dsPerson.Tables[0].DefaultView.Count == 0)
                                {
                                    throw new Exception("第" + (i + 1) + "行数据部门编码" + model.cdept_id + "不存在对应U8部门档案");
                                }
                                accvouch.cdept_id = model.cdept_id;
                            }
                        }
                        if (dscode.Tables[0].DefaultView[0]["bitem"].ObjToBoolean() == true || model.citem_class == "98")
                        {
                            if (model.citem_class.ToString() != "")
                            {
                                string accid = u8Login.get_cAcc_Id();
                                if (model.citem_id.ToString() == "")
                                {
                                    //throw new Exception("第" + ((int)i + 1) + "行数据南北项目名称为空");
                                    errstr = errstr + "第" + ((int)i + 1) + "行数据项目编码为空|";
                                    continue;
                                }



                                if (model.citem_class == "98")
                                {
                                    strsql = new StringBuilder();
                                    strsql.Append("select * from fitemss98");
                                    strsql.Append(" where citemcode='" + model.citem_id + "'");
                                    DataSet ds2 = DbHelperSQL.Query(strsql.ToString(), u8Login.UFDataConnstringForNet);
                                    if (ds2.Tables[0].DefaultView.Count == 0)
                                    {
                                        //throw new Exception("第" + ((int)i + 1) + "行数据南北项目名称" + model.citem_id + "不存在对应U8项目编码");
                                        errstr = errstr + "第" + ((int)i + 1) + "行数据项目名称" + model.citem_id + "不存在对应U8项目编码|";
                                        continue;
                                    }
                                    UFIDA.U8.DB.Model.GL_CashTable GL_CashTableU10 = new UFIDA.U8.DB.Model.GL_CashTable();
                                    GL_CashTableU10.iPeriod = accvouch.iperiod;
                                    GL_CashTableU10.iSignSeq = dsisignseq.Tables[0].DefaultView[0]["isignseq"].ObjToInt32();
                                    GL_CashTableU10.iNo_id = accvouch.ino_id;
                                    GL_CashTableU10.inid = accvouch.inid;
                                    GL_CashTableU10.cCashItem = ds2.Tables[0].DefaultView[0]["citemcode"].ToString();
                                    GL_CashTableU10.md = model.md;
                                    GL_CashTableU10.mc = model.mc;
                                    GL_CashTableU10.ccode = model.ccode;
                                    GL_CashTableU10.md_f = model.md_f;
                                    GL_CashTableU10.mc_f = (decimal)model.mc_f;
                                    GL_CashTableU10.nc_s = (decimal)model.nc_s;
                                    GL_CashTableU10.dbill_date = accvouch.dbill_date;
                                    GL_CashTableU10.csign = model.csign;
                                    GL_CashTableU10.iYPeriod = accvouch.iYPeriod;
                                    GL_CashTableU10.iyear = accvouch.iyear;
                                    commands.Add(UFIDA.U8.DB.DAL.GL_CashTable.GetAddCommand(GL_CashTableU10));

                                }
                                else
                                {
                                    accvouch.citem_id = model.citem_id;
                                    accvouch.citem_class = "00";
                                }
                            }
                        }
                        #region 自定义项
                        if (dscode.Tables[0].DefaultView[0]["bcDefine1"].ObjToBoolean() == true && model.cdefine1.ToString() != "")
                        {
                            accvouch.cDefine1 = model.cdefine1;
                        }
                        if (dscode.Tables[0].DefaultView[0]["bcDefine2"].ObjToBoolean() == true && model.cdefine2.ToString() != "")
                        {
                            accvouch.cDefine2 = model.cdefine2;
                            //}
                            if (dscode.Tables[0].DefaultView[0]["bcDefine3"].ObjToBoolean() == true && model.cdefine3.ToString() != "")
                            {
                                accvouch.cDefine3 = model.cdefine3;
                            }
                            if (dscode.Tables[0].DefaultView[0]["bcDefine4"].ObjToBoolean() == true && model.cdefine4.ToString() != "")
                            {
                                accvouch.cDefine4 = model.cdefine4;
                            }
                            if (dscode.Tables[0].DefaultView[0]["bcDefine5"].ObjToBoolean() == true && model.cdefine5.ToString() != "")
                            {
                                accvouch.cDefine5 = model.cdefine5;
                            }
                            if (dscode.Tables[0].DefaultView[0]["bcDefine6"].ObjToBoolean() == true && model.cdefine6.ToString() != "")
                            {
                                accvouch.cDefine6 = model.cdefine6;
                            }
                            if (dscode.Tables[0].DefaultView[0]["bcDefine7"].ObjToBoolean() == true && model.cdefine7.ToString() != "")
                            {
                                accvouch.cDefine7 = (decimal)model.cdefine7;
                            }
                            if (dscode.Tables[0].DefaultView[0]["bcDefine7"].ObjToBoolean() == true && model.cdefine8.ToString() != "")
                            {
                                accvouch.cDefine8 = model.cdefine8;
                            }
                            if (dscode.Tables[0].DefaultView[0]["bcDefine9"].ObjToBoolean() == true && model.cdefine9.ToString() != "")
                            {
                                accvouch.cDefine9 = model.cdefine9;
                            }
                            if (dscode.Tables[0].DefaultView[0]["bcDefine10"].ObjToBoolean() == true && model.cdefine10.ToString() != "")
                            {
                                accvouch.cDefine10 = model.cdefine10;
                            }
                            if (dscode.Tables[0].DefaultView[0]["bcDefine11"].ObjToBoolean() == true && model.cdefine11.ToString() != "")
                            {
                                accvouch.cDefine11 = model.cdefine11;
                            }
                            if (dscode.Tables[0].DefaultView[0]["bcDefine12"].ObjToBoolean() == true && model.cdefine12.ToString() != "")
                            {
                                accvouch.cDefine12 = model.cdefine12;
                            }
                            if (dscode.Tables[0].DefaultView[0]["bcDefine13"].ObjToBoolean() == true && model.cdefine13.ToString() != "")
                            {
                                accvouch.cDefine13 = model.cdefine13;
                            }
                            if (dscode.Tables[0].DefaultView[0]["bcDefine14"].ObjToBoolean() == true && model.cdefine14.ToString() != "")
                            {
                                accvouch.cDefine14 = model.cdefine14;
                            }
                            if (dscode.Tables[0].DefaultView[0]["bcDefine15"].ObjToBoolean() == true && model.cdefine15.ToString() != "")
                            {
                                accvouch.cDefine15 = model.cdefine15;
                            }
                            if (dscode.Tables[0].DefaultView[0]["bcDefine16"].ObjToBoolean() == true && model.cdefine16.ToString() != "")
                            {
                                accvouch.cDefine16 = (decimal)model.cdefine16;
                            }
                        #endregion
                        }
                        if (mccode == "" && mdcode == "")
                        {
                        }
                        else
                        {
                            if (models[i].md != 0)
                            {
                                accvouch.ccode_equal = mccode;
                            }
                            if (models[i].mc != 0)
                            {
                                accvouch.ccode_equal = mdcode;
                            }
                        }
                        accvouch.mc = model.mc;
                        accvouch.md = model.md;
                        accvouch.md_f = model.md_f;
                        accvouch.mc_f = model.mc_f;
                        accvouch.nfrat = (decimal)model.nfrat;
                        accvouch.cdigest = model.cdigest;
                        accvouch.nd_s = (decimal)model.nd_s;
                        accvouch.nc_s = (decimal)model.nc_s;
                        if (model.cexch_name != "")
                        {
                            if (model.cexch_name == "人民币" || model.cexch_name == "CNY")
                            {

                            }
                            else if (model.cexch_name == "USD")
                            {
                                accvouch.cexch_name = "美元";
                            }
                            else
                            {
                                accvouch.cexch_name = model.cexch_name;
                            }
                        }

                        accvouch.cbill = model.createby;
                        accvouch.cDefine1 = model.ino_id;
                        commands.Add(UFIDA.U8.DB.DAL.accvouch.Add(accvouch));
                    }
                    if (errstr != "")
                    {
                        throw new Exception(errstr);
                    }

                    DbHelperSQL.ExecuteSqlTran(commands, base.U8Constr);
                }
            }
            catch (Exception e)
            {
                Pub.WriteLog.WriteLine(DateTime.Now.ToShortDateString() + ":accvouchHelper_Delete_ApiResult:" + e);
                throw e;
            }
        }
        protected override void Delete(U8Login.clsLogin u8Login, List<JsonModel.accvouch> models)
        {
            if (models.Count != 1)
            { 
                if (models.Count == 0)
                {
                    throw new Exception("数据不能为空!");
                }
            }

            try
            {
                if (gl .Exists(models[0].ino_id, models[0].dbill_date.Month, models[0].dbill_date.Year, models[0].csign, base.U8Constr))
                {
                    StringBuilder strsqlino = new StringBuilder();
                    ////strsql.Append(" select * from currentstock where 1=1 and exists (select '6050','6051')");
                    strsqlino.Append("select * from GL_accvouch");
                    strsqlino.Append(" where cDefine1='" + models[0].ino_id + "' and iperiod=" + models[0].dbill_date.Month + " and iyear=" + models[0].dbill_date.Year + " and csign='" + models[0].csign + "'");
                    DataSet dsino = DbHelperSQL.Query(strsqlino.ToString(), u8Login.UFDataConnstringForNet);
                    #region 删除后
                    if (dsino.Tables[0].DefaultView[0]["ccheck"] != DBNull.Value || dsino.Tables[0].DefaultView[0]["cbook"] != DBNull.Value || dsino.Tables[0].DefaultView[0]["ibook"].ToString() != "0")
                    {
                        throw new Exception("凭证已审核或记账,在U8的" + models[0].csign + "的" + dsino.Tables[0].DefaultView[0]["ino_id"].ToString() + ",无法删除");
                    }
                    if (UFIDA.U8.DB.DAL.GL_CashTable.Exists(dsino.Tables[0].DefaultView[0]["ino_id"].ToString(), models[0].dbill_date.Month, models[0].dbill_date.Year, models[0].csign, base.U8Constr))
                    {
                        commands.Add(UFIDA.U8.DB.DAL.GL_CashTable.DeleteCommand(dsino.Tables[0].DefaultView[0]["ino_id"].ToString(), models[0].dbill_date.Month, models[0].dbill_date.Year, models[0].csign));
                    }
                    commands.Add(UFIDA.U8.DB.DAL.accvouch.Delete(models[0].ino_id, models[0].dbill_date.Month, models[0].dbill_date.Year, models[0].csign));
                    if (errstr != "")
                    {
                        throw new Exception(errstr);
                    }

                    DbHelperSQL.ExecuteSqlTran(commands, base.U8Constr);
                    billid = "";
                    #endregion
                }
                else
                {
                    throw new Exception("凭证不存在");
                }
            }
            catch (Exception e)
            {
                Pub.WriteLog.WriteLine(DateTime.Now.ToShortDateString() + ":accvouchHelper_Delete_ApiResult:" + e);
                throw e;
            } 
        }
    }
}
